package com.company.manage.system.web.controller;

import com.company.manage.common.base.BaseController;
import com.company.manage.common.utils.Exceptions;
import com.company.manage.system.entity.Permission;
import com.company.manage.system.service.PermissionService;
import com.company.manage.system.service.RolePermissionService;
import com.company.manage.system.utils.UserUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HuangYong on 2016/1/23.
 * desc: 权限controller
 */
@Controller
@RequestMapping("system/permission")
public class PermissionController extends BaseController {
    private static final String TAG = "PermissionController";

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 加载菜单页
     *
     * @return
     */
    @RequestMapping(value = "menu", method = RequestMethod.GET)
    public String menuList() {
        return "system/menuList";
    }

    /**
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list() {
        return "system/permissionList";
    }

    /**
     * 加载所有的菜单集合 JSON
     *
     * @return
     */
    @RequiresPermissions("sys:perm:menu:view")
    @RequestMapping(value = "menu/json", method = RequestMethod.GET)
    @ResponseBody
    public List<Permission> menuData() {
        List<Permission> lists = permissionService.getMenus();
        return lists;
    }

    /**
     * 获取菜单下的操作
     *
     * @param pid
     * @return
     */
    @RequiresPermissions("sys:perm:view")
    @RequestMapping("ope/json")
    @ResponseBody
    public Map<String, Object> menuOperationData(Integer pid) {
        List<Permission> moLists = permissionService.getMenuOperation(pid);

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("rows", moLists);
        map.put("total", moLists.size());
        return map;
    }

    /**
     * 获取所有的权限集合 JSON
     *
     * @return
     */
    @RequiresPermissions("sys:perm:view")
    @RequestMapping(value = "json", method = RequestMethod.GET)
    @ResponseBody
    public List<Permission> getData() {
        List<Permission> lists = permissionService.getAll();
        return lists;
    }

    /**
     * 获取当前用户的权限集合JSON
     *
     * @return
     */
    @RequestMapping("i/json")
    @ResponseBody
    public List<Permission> myPermissionData() {

        try {
            List<Permission> lists = permissionService.getPermissions(UserUtil.getCurrentUser().getId());
            return lists;
        } catch (Exception e) {
            e.printStackTrace();
            throw Exceptions.unchecked(e);
        }
    }

    /**
     * 获取某用户的权限集合 JSON
     *
     * @param userId
     * @return
     */
    @RequiresPermissions("sys:perm:view")
    @RequestMapping("{userId}/json")
    @ResponseBody
    public List<Permission> otherPermissionData(@PathVariable("userId") Integer userId) {
        List<Permission> list = permissionService.getPermissions(userId);
        return list;
    }

    /**
     * 添加权限跳转
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("permission", new Permission());
        model.addAttribute("action", "create");
        return "system/permissionForm";
    }

    /**
     * 添加菜单跳转
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "menu/create", method = RequestMethod.GET)
    public String menuCreateForm(Model model) {
        model.addAttribute("permission", new Permission());
        model.addAttribute("action", "create");
        return "system/menuForm";
    }

    /**
     * 添加权限、菜单
     *
     * @param permission
     * @param model
     * @return
     */
    @RequiresPermissions("sys:perm:add")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@Valid Permission permission, Model model) {
        permissionService.save(permission);
        return "success";
    }

    /**
     * 添加菜单基础操作
     *
     * @param pname
     * @param pid
     * @param model
     * @return
     */
    @RequiresPermissions("sys:perm:add")
    @RequestMapping(value = "createBase/{pname}/{pid}", method = RequestMethod.GET)
    @ResponseBody
    public String create(@PathVariable("pname") String pname, @PathVariable("pid") Integer pid, Model model) {
        permissionService.addBaseOperation(pid, pname);
        return "success";
    }

    /**
     * 修改权限跳转
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("permission", permissionService.get(id));
        model.addAttribute("action", "update");
        return "system/permissionForm";
    }

    /**
     * 修改菜单跳转
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "menu/update/{id}", method = RequestMethod.GET)
    public String updateMenuForm(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("permission", permissionService.get(id));
        model.addAttribute("action", "update");
        return "system/menuForm";
    }

    /**
     * 修改权限、菜单
     *
     * @param permission
     * @param model
     * @return
     */
    @RequiresPermissions("sys:perm:update")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@Valid @ModelAttribute("permission") Permission permission, Model model) {
        permissionService.save(permission);
        return "success";
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @RequiresPermissions("sys:perm:delete")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable("id") Integer id) {
        permissionService.delete(id);
        return "success";
    }

    @ModelAttribute
    public void getPermission(@RequestParam(value = "id", defaultValue = "-1") Integer id, Model model) {
        if (id != null) {
            model.addAttribute("permission", permissionService.get(id));
        }
    }


}